<?php

require_once(sfConfig::get('sf_lib_dir').'/filter/base/BaseFormFilterPropel.class.php');

/**
 * Question filter form base class.
 *
 * @package    elearning
 * @subpackage filter
 * @author     Your name here
 */
class BaseQuestionFormFilter extends BaseFormFilterPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'level_id'           => new sfWidgetFormPropelChoice(array('model' => 'Level', 'add_empty' => true)),
      'teacher_id'         => new sfWidgetFormPropelChoice(array('model' => 'Teacher', 'add_empty' => true)),
      'question_type_id'   => new sfWidgetFormPropelChoice(array('model' => 'QuestionType', 'add_empty' => true)),
      'question_subject'   => new sfWidgetFormFilterInput(),
      'question_content'   => new sfWidgetFormFilterInput(),
      'mark'               => new sfWidgetFormFilterInput(),
      'question_status'    => new sfWidgetFormFilterInput(),
      'created_at'         => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)),
      'updated_at'         => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)),
      'question_test_list' => new sfWidgetFormPropelChoice(array('model' => 'Test', 'add_empty' => true)),
    ));

    $this->setValidators(array(
      'level_id'           => new sfValidatorPropelChoice(array('required' => false, 'model' => 'Level', 'column' => 'id')),
      'teacher_id'         => new sfValidatorPropelChoice(array('required' => false, 'model' => 'Teacher', 'column' => 'id')),
      'question_type_id'   => new sfValidatorPropelChoice(array('required' => false, 'model' => 'QuestionType', 'column' => 'id')),
      'question_subject'   => new sfValidatorPass(array('required' => false)),
      'question_content'   => new sfValidatorPass(array('required' => false)),
      'mark'               => new sfValidatorSchemaFilter('text', new sfValidatorNumber(array('required' => false))),
      'question_status'    => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'created_at'         => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'updated_at'         => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'question_test_list' => new sfValidatorPropelChoice(array('model' => 'Test', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('question_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function addQuestionTestListColumnCriteria(Criteria $criteria, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $criteria->addJoin(QuestionTestPeer::QUESTION_ID, QuestionPeer::ID);

    $value = array_pop($values);
    $criterion = $criteria->getNewCriterion(QuestionTestPeer::TEST_ID, $value);

    foreach ($values as $value)
    {
      $criterion->addOr($criteria->getNewCriterion(QuestionTestPeer::TEST_ID, $value));
    }

    $criteria->add($criterion);
  }

  public function getModelName()
  {
    return 'Question';
  }

  public function getFields()
  {
    return array(
      'id'                 => 'Number',
      'level_id'           => 'ForeignKey',
      'teacher_id'         => 'ForeignKey',
      'question_type_id'   => 'ForeignKey',
      'question_subject'   => 'Text',
      'question_content'   => 'Text',
      'mark'               => 'Number',
      'question_status'    => 'Number',
      'created_at'         => 'Date',
      'updated_at'         => 'Date',
      'question_test_list' => 'ManyKey',
    );
  }
}
